package cn.edu.stu.syllabus.widgets;

import android.content.Context;
import android.os.Build;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by daidaijie on 2016/9/13.
 */
public class ScrollAwareTopViewBehavior extends CoordinatorLayout.Behavior {

    public ScrollAwareTopViewBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        // 忽略4.4以下的滑动处理
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
                (nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
                        super.onStartNestedScroll(coordinatorLayout, child,
                                directTargetChild, target, nestedScrollAxes));
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        if (dyConsumed > 0 && child.getVisibility() == View.INVISIBLE) {
            // User scrolled down and the FAB is currently visible -> hide the FAB
            child.setVisibility(View.VISIBLE);
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            // User scrolled up and the FAB is currently not visible -> show the FAB
            child.setVisibility(View.INVISIBLE);
        }
    }
}

